/*
 *  DataBaseConstants.h
 *  MarketDataColloctor
 *
 *  Created by jiro on 10/01/14.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#import <Cocoa/Cocoa.h>

const NSString* const kText_MarketDatabase			= @"MarketDatabase";

const NSString* const kText_StockExchangeJaTable				= @"stock_exchange_ja_table";
const NSString* const kText_StockBrandJaTable					= @"stock_brand_ja_table";
const NSString* const kText_StockBrandCategoryJaTable			= @"stock_brand_category_ja_table";
const NSString* const kText_StockUnitDivisionJaTable			= @"stock_unit_division_ja_table";
const NSString* const kText_StockCorrelationCoefficientJaTable	= @"stock_correlation_coefficient_ja_table";
const NSString* const kText_AnalyzeScheduleTable				= @"analyze_schedule_table";
const NSString* const kText_AnalyzeTypeTable					= @"analyze_type_table";
const NSString* const kText_StockConfidenceRemainJaTable		= @"stock_confidence_remain_ja_table";
const NSString* const kText_CurrencyTypeTable					= @"currency_type_table";
const NSString* const kText_CurrencyExchangeTypeTable			= @"currency_exchange_type_table";
const NSString* const kText_CurrencyExchangeJaTable				= @"currency_exchange_ja_table";
const NSString* const kText_CurrencyExchangeJaMaTable			= @"currency_exchange_ja_ma_table";

const NSString* const kText_CurrencyExchangeGmoTable			= @"currency_exchange_gmo_table";
const NSString* const kText_CurrencyExchangeGmoMaTable			= @"currency_exchange_gmo_ma_table";
const NSString* const kText_CurrencyExchangeLiquidiyGmoTable	= @"currency_exchange_liquidiy_gmo_table";
const NSString* const kText_CurrencyExchangeLiquidiyGmoMaTable	= @"currency_exchange_liquidiy_gmo_ma_table";

const NSString* const kText_DataCollectLogTable					= @"data_collect_log_table";
const NSString* const kText_CodeTable							= @"code_table";
const NSString* const kText_JaNationalBonds						= @"national_bonds_ja_table";
const NSString* const kText_JaNationalBondsExchange				= @"national_bonds_exchange_ja_table";
const NSString* const kText_StockExchangeJaBasicStaticsTable	= @"stock_exchange_ja_basic_statics_table";
const NSString* const kText_CrudeOilProductCountryTable			= @"crude_oil_product_country_table";
const NSString* const kText_CrudeOilCodeTable					= @"crude_oil_code_table";
const NSString* const kText_CrudeOilExchangeTable				= @"crude_oil_exchange_table";

const NSString* const kText_StrategyInspectionParameterTable	= @"strategy_inspection_parameter_table";
const NSString* const kText_StrategyInspectionResultTable		= @"strategy_inspection_result_table";

const NSString* const kText_BrokerageFirmTable					= @"brokerage_firm_table";

const NSString* const kText_StrategyParameterSet005Table		= @"strategy_parameter_set_005_table";
const NSString* const kText_FxBasicInfo							= @"fx_basic_info_table";
const NSString* const kText_StrategyPerfomance					= @"strategy_perfomance_table";

const NSString* const kText_ExchangeDate			= @"exchange_date";
const NSString* const kText_OpenPrice				= @"open_price";
const NSString* const kText_HighPrice				= @"high_price";
const NSString* const kText_LowPrice				= @"low_price";
const NSString* const kText_ClosePrice				= @"close_price";
const NSString* const kText_Volume					= @"volume";
const NSString* const kText_AdjustRate				= @"adjust_rate";
const NSString* const kText_AdjustedEndPrice		= @"adjusted_price";
const NSString* const kText_BrandName				= @"brand_name";
const NSString* const kText_MarketInitial			= @"market_initial";
const NSString* const kText_ExistFlag				= @"exist_flag";
const NSString* const kText_LastExchangeDataCollected
													= @"last_exchange_data_collected";
const NSString* const kText_Price					= @"price";
const NSString* const kText_BrandCode1				= @"brand_code1";
const NSString* const kText_StartDate				= @"start_date";
const NSString* const kText_StartDate1				= @"start_date1";
const NSString* const kText_EndDate					= @"end_date";
const NSString* const kText_EndDate1				= @"end_data1";
const NSString* const kText_BrandCode2				= @"brand_code2";
const NSString* const kText_StartDate2				= @"start_date2";
const NSString* const kText_EndDate2				= @"end_date2";
const NSString* const kText_StartCode				= @"start_code";
const NSString* const kText_EndCode					= @"end_code";
const NSString* const kText_CorrelationCoefficient	= @"correlation_coefficient";
const NSString* const kText_ValidDataCount			= @"valid_data_count";
const NSString* const kText_AnalyzeType				= @"analyze_type";
const NSString* const kText_OptionDate1				= @"option_date1";
const NSString* const kText_OptionDate2				= @"option_date2";
const NSString* const kText_OptionDate3				= @"option_date3";
const NSString* const kText_OptionDate4				= @"option_date4";
const NSString* const kText_OptionString1			= @"option_string1";
const NSString* const kText_OptionString2			= @"option_string2";
const NSString* const kText_CompleteFlag			= @"complete_flag";
const NSString* const kText_Type					= @"type";
const NSString* const kText_Name					= @"name";
const NSString* const kText_DivisionDate			= @"division_date";
const NSString* const kText_UnitDivided				= @"unit_divided";
const NSString* const kText_DataType				= @"data_type";
const NSString* const kText_OptionString			= @"option_string";
const NSString* const kText_LatestDate				= @"latest_date";
const NSString* const kText_SellRemain				= @"sell_remain";
const NSString* const kText_BuyRemain				= @"buy_remain";
const NSString* const kText_SellVariation			= @"sell_variation";
const NSString* const kText_BuyVariation			= @"buy_variation";
const NSString* const kText_ConfidenceRate			= @"confidence_rate";
const NSString* const kText_Code					= @"code";
const NSString* const kText_Code0					= @"code0";
const NSString* const kText_Code1					= @"code1";
const NSString* const kText_JapaneseName			= @"japanese_name";
const NSString* const kText_CurrencyCode			= @"currency_code";
const NSString* const kText_TargetCurrencyCode		= @"target_currency_code";
const NSString* const kText_MarketDataType			= @"market_data_type";
const NSString* const kText_InterestRate			= @"interest_rate";

const NSString* const kText_AverageLength			= @"average_length"; 
const NSString* const kText_Liquidity				= @"liquidiy";
const NSString* const kText_Index					= @"index";

const NSString* const kText_DaysInterval			= @"days_interval";
const NSString* const kText_DataCount				= @"data_count";
const NSString* const kText_Mean					= @"mean";
const NSString* const kText_Variance				= @"variance";
const NSString* const kText_Max						= @"max";
const NSString* const kText_Min						= @"min";
const NSString* const kText_StandardDeviation		= @"standard_deviation";
const NSString* const kText_DeltaRateMean			= @"delta_rate_mean";
const NSString* const kText_DeltaRateVariance		= @"delta_rate_variance";
const NSString* const kText_DeltaRateStandardDeviation
													= @"delta_rate_standard_deviation";
const NSString* const kText_DeltaRateMin			= @"delta_rate_min";
const NSString* const kText_DeltaRateMax			= @"delta_rate_max";
const NSString* const kText_DeltaStandardDeviation	= @"delta_standart_deviation";
const NSString* const kText_CrudeOilProductCountryCode
													= @"crude_oil_product_country_code";
const NSString* const kText_CrudeOilCode			= @"crude_oil_code";
const NSString* const kText_ShortTermMaLength		= @"short_term_ma_length";
const NSString* const kText_MiddleTermMaLength		= @"middle_term_ma_length";
const NSString* const kText_LongTermMaLength		= @"long_term_ma_length";
const NSString* const kText_TargetSettlementGap		= @"target_settlement_gap";
const NSString* const kText_LossCutGap				= @"loss_cut_gap";
const NSString* const kText_PriceFieldType			= @"price_field_type";
const NSString* const kText_InvestmentPositionType	= @"investment_position_type";
const NSString* const kText_TimeUnitSec				= @"time_unit_sec";

const NSString* const kText_InvestMoney				= @"invest_money";
const NSString* const kText_TargetInterest			= @"target_interest";
const NSString* const kText_TolerenctLossRate		= @"tolerenct_loss_rate";
const NSString* const kText_Leverage				= @"leverage";
const NSString* const kText_EntryExchange			= @"entry_exchange";
const NSString* const kText_TargetExchangeRateGap	= @"target_exchange_rate_gap";
const NSString* const kText_LossCutExchangeRateGap	= @"loss_cut_exchange_rate_gap";
const NSString* const kText_Spread					= @"spread";
const NSString* const kText_Charge					= @"charge";
const NSString* const kText_TradeCount				= @"trade_count";
const NSString* const kText_WinRate					= @"win_rate";
const NSString* const kText_ProfitPerTrade			= @"profit_per_trade";
const NSString* const kText_TimeIntervalPerTrade	= @"time_interval_per_trade";
const NSString* const kText_InspectParameterIndex	= @"inspect_parameter_index";
const NSString* const kText_StrategyIndex			= @"strategy_index";
const NSString* const kText_ParameterIndex			= @"parameter_index";
const NSString* const kText_DayIntervalPerTrade		= @"day_interval_per_trade";

const NSString* const kText_BrokerageFirmIndex		= @"brokarage_firm_index";

const NSString* const kText_JPY						= @"JPY";
const NSString* const kText_USD						= @"USD";
const NSString* const kText_EUR						= @"EUR";
const NSString* const kText_AUD						= @"AUD";
const NSString* const kText_NZD						= @"NZD";
const NSString* const kText_GBP						= @"GBP";
const NSString* const kText_CAD						= @"CAD";
const NSString* const kText_CHF						= @"CHF";

const NSString* const kText_DefaultDb				= @"MarketDatabase";
const NSString* const kText_DefaultHost				= @"localhost";
const NSString* const kText_DefaultUserName			= @"jiro";

const NSString* const kText_Text					= @"text";
const NSString* const kText_TimestampWithTimeZone	= @"timestamp with time zone";
const NSString* const kText_Real					= @"real";
const NSString* const kText_Integer					= @"integer";
const NSString* const kText_PrimaryKey				= @"primary key";
const NSString* const kText_OrderBy					= @"order by";
const NSString* const kText_VarChar					= @"varchar";
const NSString* const kText_Boolean					= @"boolean";
const NSString* const kText_Serial					= @"serial";

const NSString* const kText_Coalesce				= @"coalesce";
const NSString* const kText_Count					= @"count";
const NSString* const kText_CountForAsterisk		= @"count(*)";

class PriceField
{	
public:
	enum Type
	{
		Type_StartPrice,
		Type_EndPrice,
		Type_HighestPrice,
		Type_LowestPrice,
		Type_MiddleBetweenStartAndEnd,
		Type_MiddleBetweenHighAndLow,
		Type_Count,
	};

	static NSString* toString( PriceField::Type type )
	{
		switch ( type )
		{
		case Type_StartPrice:
			return kText_OpenPrice;
		case Type_EndPrice:
			return kText_ClosePrice;
		case Type_HighestPrice:
			return kText_HighPrice;
		case Type_LowestPrice:
			return kText_LowPrice;
		case Type_MiddleBetweenStartAndEnd:
			return [ NSString stringWithFormat: @"( ( %@ + %@ ) / 2.0 )", kText_OpenPrice, kText_ClosePrice ];
		case Type_MiddleBetweenHighAndLow:
			return [ NSString stringWithFormat: @"( ( %@ + %@ ) / 2.0 )", kText_HighPrice, kText_LowPrice ];

		default:
			return @"";
		}
	}
	
	static Type toType( NSString* s )
	{
		for ( int i = 0; i < Type_Count; i++ )
		{
			Type type = static_cast< Type >( i );
			if ( [ s isEqualToString: toString( type ) ] )
			{
				return type;
			}
		}
		return Type_Count;
	}
};



class MarketData
{
public:
	enum Type
	{
		Type_StockExchangeJa,
		
		Type_StockConfidenceRemainJa,

		Type_CurrencyExchangeJa,

		Type_NationalBondsJa,

		Type_CrudeOil,
		
		Type_Count,
	};
};

enum DbId
{
	Db_MargetDatabase,
	
	Db_Invalid,
	
};

enum TableId
{
	Table_StockExchangeJaTable,

	Table_StockUnitDvisionTable,

	Table_StockCorrelationCoefficientJaTable,

	Table_AnalyzeLogTable,
	
	Table_AnalyzeTypeTable,

	Table_CurrencyTypeTable,

	Table_CurrencyExchangeTypeJaTable,

	Table_CurrenctExchangeJaTable,

	Table_DataCollectLogTable,
	
	Table_CodeTable,

	Table_Invalid,
	
};

enum FieldId
{
	Field_BrandCode,
	
	Field_ExchangeDate,
	
	Field_StartPrice,
	
	Field_EndPrice,
	
	Field_HighestPrice,

	Field_LowestPrice,
	
	Field_Volume,
	
	Field_AdjustRate,
	
	Field_ExistFlag,
	
	Field_BrandName,

	Field_MarketInitial,

	Field_LastDailyExchangeDataCollectDate,

	Field_DivisionDate,
	
	Field_UnitDivided,

	Field_BrandCode1,
	
	Field_StartDate1,
	
	Field_EndDate1,
	
	Field_BrandCode2,
	
	Field_StartDate2,
	
	Field_EndDate2,
	
	Field_CorrelationCoefficient,
	
	Field_ValidDataCount,

	Field_AnalyzeType,
	
	Field_OptionDate1,
	
	Field_OptionDate2,
	
	Field_OptionDate3,
	
	Field_OptionDate4,
	
	Field_OptionString1,
	
	Field_OptionString2,
	
	Field_CompleteFlag,
	
	Field_Type,
	
	Field_Name,

	Field_DataType,
	
	Field_OptionString,
	
	Field_LatestDate,

	Field_SellRemain,
	
	Field_BuyRemain,
	
	Field_SellVariation,
	
	Field_BuyVariation,
	
	Field_ConfidenceRate,

	Field_Code,

	Field_JapaneseName,

	Field_TargetCurrencyCode,

	Field_MarketDataType,
	
	Field_InterestRate,

	Field_Invalid,

};
